#ifndef CSDLTIMEKEEPER_H_
#define CSDLTIMEKEEPER_H_

#include "../bengine/CTimekeeper.h"
#include <SDL/SDL.h>
#include <SDL/SDL_framerate.h>

namespace FBShells {
	class CSDLTimekeeper : public CTimekeeper {
		protected:
			FPSmanager* m_FPSManager;
			unsigned int _curTime;
			unsigned int m_FPS;
			unsigned int _FPS_counter;
			unsigned int _curframeTime;
		public:
			CSDLTimekeeper() { m_FPSManager = new FPSmanager; _curTime = SDL_GetTicks(); _curframeTime = _curTime; m_FPS = 0; }
			virtual ~CSDLTimekeeper() { delete m_FPSManager; }
			virtual bool initialize() {	SDL_initFramerate(m_FPSManager); return true; }
			virtual void shutdown() { }
			void setFPS(int fps) { SDL_setFramerate(m_FPSManager, fps); }
			int getFPS() { return m_FPS; }
			int getTimeDelta() { unsigned int delta = SDL_GetTicks()-_curTime; _curTime=SDL_GetTicks(); return delta; } 
			void renderDelay() { 
				SDL_framerateDelay(m_FPSManager); 
				_FPS_counter++;
				if(SDL_GetTicks() - _curframeTime > 1000) {
					m_FPS = int((_FPS_counter*1000)/(SDL_GetTicks() - _curframeTime));
					_curframeTime = SDL_GetTicks();
					_FPS_counter = 0;
				}
			}
	};
}

#endif /*CSDLTIMEKEEPER_H_*/
